<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https=>//www.hyperf.io
 * @document https=>//hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https=>//github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller\Admin;

use App\Constants\ErrorCode;
use Hyperf\HttpServer\Annotation\Controller;
use Core\Repository\Admin\UserRepository;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use App\Middleware\AdminAuthMiddleware;
use App\Middleware\LoginAuthMiddleware;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Exception\BussniessException;

/**
 * Permission
 * 首页控制器
 * @package App\Controller\Admin
 * @Controller(prefix="/permission")
 * @Middlewares({
 *     @Middleware(LoginAuthMiddleware::class),
 *     @Middleware(AdminAuthMiddleware::class)
 * })
 * @property \Core\Repository\Admin\PermissionRepository $permissionRepo
 */
class PermissionController extends BaseController
{
    /**
     * getUserPermissions
     * 获取当前用户拥有的权限
     * @return \Psr\Http\Message\ResponseInterface
     * @PostMapping(path="user_permissions")
     */
    public function getUserPermissions()
    {
        $list = $this->permissionRepo->getUserPermissionsList();

        $data = [
            'list' => $list
        ];

        return $this->response->success($data);
    }

    /**
     * index
     * 权限列表，权限管理
     * @return \Psr\Http\Message\ResponseInterface
     * @GetMapping(path="list")
     */
    public function index()
    {
        $list = $this->permissionRepo->getPermissionsList();

        $data = [
            'list' => $list
        ];

        return $this->response->success($data);
    }

    /**
     * store
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\SimpleCache\InvalidArgumentException
     *
     * @PostMapping(path="store")
     */
    public function store()
    {
        try {
            $reqParam = $this->request->all();
            $id = $this->permissionRepo->savePermissions($reqParam);

            return $this->response->success($id);
        } catch (\Throwable $throwable) {
            throw new BussniessException(ErrorCode::ERR_EXCEPTION_USER,$throwable->getMessage());
        }
    }

    /**
     * getInfo
     * 根据id获取单条记录信息
     * @return \Psr\Http\Message\ResponseInterface
     *
     * @PostMapping(path="get_info")
     */
    public function getInfo()
    {
        $reqParam = $this->request->all();
        $info = $this->permissionRepo->getInfo($reqParam['id']);

        $data = [
            'info' => $info,
        ];

        return $this->response->success($data);
    }

    /**
     * orderPermissions
     * 权限的拖拽排序
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\SimpleCache\InvalidArgumentException
     *
     * @PostMapping(path="order")
     */
    public function orderPermissions()
    {
        $reqParam = $this->request->all();
        $this->permissionRepo->orderPermissions($reqParam['ids']);

        return $this->response->success('ok');
    }

    /**
     * destroy
     * 删除权限
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\SimpleCache\InvalidArgumentException
     *
     * @PostMapping(path="delete")
     */
    public function destroy()
    {
        $reqParam = $this->request->all();
        $this->permissionRepo->deleteInfo($reqParam['id']);

        return $this->response->success('ok');
    }

}
